package it.muia.ricchefacce.renderkit;

import it.muia.ricchefacce.component.AbstractSignpad;
import it.muia.ricchefacce.utils.ImageGenerator;

import java.util.Map;

import javax.faces.application.ResourceDependencies;
import javax.faces.application.ResourceDependency;
import javax.faces.component.UIComponent;
import javax.faces.context.ExternalContext;
import javax.faces.context.FacesContext;
import javax.faces.convert.ConverterException;

import org.richfaces.renderkit.InputRendererBase;
import org.richfaces.renderkit.RenderKitUtils;

@ResourceDependencies(
{ @ResourceDependency(library = "org.richfaces", name = "ajax.reslib"), @ResourceDependency(name = "richfaces-base-component.js"),
		@ResourceDependency(name = "jquery.position.js"), @ResourceDependency(name = "richfaces-event.js"),
		@ResourceDependency(library = "org.richfaces", name = "json-dom.js"),
		@ResourceDependency(library = "org.richfaces", name = "jquery.effects.core.js"),
		@ResourceDependency(library = "org.richfaces", name = "jquery.effects.highlight.js"),
		@ResourceDependency(library = "org.richfaces", name = "JQuerySpinBtn.js"),
		@ResourceDependency(library = "it.muia.ricchefacce.components.signpad", name = "signpad.js"),
		@ResourceDependency(library = "it.muia.ricchefacce.components.signpad", name = "signpad.ecss"),
		@ResourceDependency(library = "it.muia.ricchefacce.components.signpad", name = "signpad-clear-btn.png"),
		@ResourceDependency(library = "it.muia.ricchefacce.components.signpad", name = "excanvas.compiled.js")

})
public class SignpadBaseRenderer extends InputRendererBase
{
	private static final String inputPartialId = ":MainDiv:InputSigns";

	private static final String SIGNPAD_CLEAR_ICON_RESOURCE_NAME = "signpad-clear-btn.png";

	public String getClearButtonIcon(FacesContext facesContext, UIComponent component)
	{
		String buttonIcon = (String) component.getAttributes().get("clearButtonIcon");
		if (buttonIcon != null && buttonIcon.trim().length() != 0)
		{
			buttonIcon = RenderKitUtils.getResourceURL(buttonIcon, facesContext);
		}
		else
		{
			buttonIcon = RenderKitUtils.getResourcePath(facesContext, "it.muia.ricchefacce.components.signpad", SIGNPAD_CLEAR_ICON_RESOURCE_NAME);
		}
		return buttonIcon;
	}

	@Override
	public String getInputValue(FacesContext facesContext, UIComponent component)
	{
		if (!(component instanceof AbstractSignpad))
		{
			return null;
		}
		AbstractSignpad signpad = (AbstractSignpad) component;
		String valueString = signpad.getSignData();
		return valueString;
	}

	@Override
	protected void doDecode(FacesContext context, UIComponent component)
	{
		ExternalContext external = context.getExternalContext();
		Map<String, String> requestParams = external.getRequestParameterMap();
		AbstractSignpad signpad = (AbstractSignpad) component;
		String clientId = signpad.getClientId(context);
		String submittedValue = requestParams.get(clientId + inputPartialId);
		if (submittedValue != null)
		{
			signpad.setSubmittedValue(submittedValue);
			signpad.setSignData(submittedValue);
		}
	}

	@Override
	public Object getConvertedValue(FacesContext context, UIComponent component, Object val) throws ConverterException
	{
		System.out.println("VALUE = [" + val + "]");
		if (val != null && !val.toString().isEmpty())
		{
			byte[] image = ImageGenerator.generateImage(val.toString());
			return image;
		}
		return null;
	}
}
